package com.taotao.portal.service.ipml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.pojo.TbContent;
import com.taotao.portal.service.IContentService;

@Service
public class ContentServiceIpml implements IContentService {
	@Value(value = "${REST_BASE_URL}")
	private String REST_BASE_URL;

	@Value(value = "${REST_INDEX_AD_URL}")
	private String REST_INDEX_AD_URL;

	/**
	 * 获取首页广告
	 */
	@Override
	public String getContentStrs() {
		// TODO Auto-generated method stub
		// 请求广告数据
		String result = HttpClientUtil.doGet(REST_BASE_URL + REST_INDEX_AD_URL);
		// 通过json进行解析
		TaotaoResult taotaoResult = JsonUtils.jsonToPojo(result,
				TaotaoResult.class);
		// 创建map构建广告信息
		List<Map<String, Object>> contentMaps = new ArrayList<Map<String, Object>>();
		@SuppressWarnings("unchecked")
		List<LinkedHashMap<String, Object>> contents = (List<LinkedHashMap<String, Object>>) taotaoResult
				.getData();
		for (LinkedHashMap<String, Object> content : contents) {
			Map<String, Object> contentMap = new HashMap<String, Object>();
			contentMap.put("srcB", content.get("pic2"));
			contentMap.put("height", 240);
			contentMap.put("alt", content.get("subTitle"));
			contentMap.put("width", 670);
			contentMap.put("src", content.get("pic"));
			contentMap.put("widthB", 550);
			contentMap.put("href", content.get("url"));
			contentMap.put("heightB", 240);
			contentMaps.add(contentMap);
		}

		return JsonUtils.objectToJson(contentMaps);
	}
}
